package cn.broadmax.boot.util;

import cn.broadmax.boot.exception.WebException;
import jexx.util.StringUtil;

import java.util.Collection;
import java.util.Map;

/**
 * 参数校验器
 * @author jeff
 * @date 2019/12/5
 */
public final class ArgChecker {

    public static final String ILLEGAL_PARAMETER = "参数不合法";
    public static final boolean DEFAULT_SUPPORT_I18N = false;

    private static final ThreadLocal<ArgChecker> local = new ThreadLocal<>();

    /** 是否支持提示消息国际化 */
    private boolean supportI18n;

    /**
     * 返回一个默认是否国际化 的参数校验器
     * @return 参数校验器
     */
    public static ArgChecker get(){
        return get(DEFAULT_SUPPORT_I18N);
    }

    /**
     * 返回一个 需国际化 的参数校验器
     * @return 参数校验器
     */
    public static ArgChecker i18n(){
        return get(true);
    }

    /**
     * 返回一个 无需国际化 的参数校验器
     * @return 参数校验器
     */
    public static ArgChecker noI18n(){
        return get(false);
    }

    private static ArgChecker get(boolean supportI18n){
        ArgChecker checker = local.get();
        if(checker == null){
            checker = new ArgChecker();
            local.set(checker);
        }
        checker.supportI18n = supportI18n;
        return checker;
    }

    public void isTrue(boolean expression, String message, Object... args) {
        if (!expression) {
            throwWebException(message, args);
        }
    }

    public void isTrue(boolean expression) {
        isTrue(expression, ILLEGAL_PARAMETER);
    }

    public void isFalse(boolean expression, String message, Object... args) {
        if (expression) {
            throwWebException(message, args);
        }
    }

    public void isFalse(boolean expression) {
        isFalse(expression, ILLEGAL_PARAMETER);
    }

    public void isNull(Object object, String message, Object... args) {
        if (object != null) {
            throwWebException(message, args);
        }
    }

    public void isNull(Object object) {
        isNull(object, ILLEGAL_PARAMETER);
    }

    public void notNull(Object object, String message, Object... args) {
        if (object == null) {
            throwWebException(message, args);
        }
    }

    public void notNull(Object object) {
        notNull(object, ILLEGAL_PARAMETER);
    }

    public void hasLength(String text) {
        hasLength(text, ILLEGAL_PARAMETER);
    }

    public void hasLength(String text, String message, Object... args) {
        if (!StringUtil.isNotBlank(text)) {
            throwWebException(message, args);
        }
    }

    public void hasText(String text, String message, Object... args) {
        if (!StringUtil.isNotBlank(text)) {
            throwWebException(message, args);
        }
    }

    public void hasText(String text) {
        hasText(text, ILLEGAL_PARAMETER);
    }

    public <T> void isEmpty(T[] array) {
        notEmpty(array, ILLEGAL_PARAMETER);
    }

    public <T> void isEmpty(T[] array, String message, Object... args) {
        if (array != null && array.length > 0) {
            throwWebException(message, args);
        }
    }

    public void isEmpty(Collection<?> collection) {
        notEmpty(collection, ILLEGAL_PARAMETER);
    }

    public void isEmpty(Collection<?> collection, String message, Object... args) {
        if (collection != null && collection.size() > 0) {
            throwWebException(message, args);
        }
    }

    public void isEmpty(Map<?, ?> map) {
        notEmpty(map, ILLEGAL_PARAMETER);
    }

    public void isEmpty(Map<?, ?> map, String message, Object... args) {
        if (map != null && map.size() > 0) {
            throwWebException(message, args);
        }
    }

    public <T> void notEmpty(T[] array) {
        notEmpty(array, ILLEGAL_PARAMETER);
    }

    public <T> void notEmpty(T[] array, String message, Object... args) {
        if (array == null || array.length == 0) {
            throwWebException(message, args);
        }
    }

    public void noNullElements(Object[] array) {
        noNullElements(array, ILLEGAL_PARAMETER);
    }

    public void noNullElements(Object[] array, String message, Object... args) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    throwWebException(message, args);
                }
            }
        }
    }

    public void notEmpty(long[] array) {
        notEmpty(array, ILLEGAL_PARAMETER);
    }

    public void notEmpty(long[] array, String message, Object... args) {
        if (array == null || array.length == 0) {
            throwWebException(message, args);
        }
    }
    public void notEmpty(int[] array) {
        notEmpty(array, ILLEGAL_PARAMETER);
    }

    public void notEmpty(int[] array, String message, Object... args) {
        if (array == null || array.length == 0) {
            throwWebException(message, args);
        }
    }

    public void notEmpty(short[] array) {
        notEmpty(array, ILLEGAL_PARAMETER);
    }

    public void notEmpty(short[] array, String message, Object... args) {
        if (array == null || array.length == 0) {
            throwWebException(message, args);
        }
    }

    public void notEmpty(char[] array) {
        notEmpty(array, ILLEGAL_PARAMETER);
    }

    public void notEmpty(char[] array, String message, Object... args) {
        if (array == null || array.length == 0) {
            throwWebException(message, args);
        }
    }

    public void notEmpty(byte[] array) {
        notEmpty(array, ILLEGAL_PARAMETER);
    }

    public void notEmpty(byte[] array, String message, Object... args) {
        if (array == null || array.length == 0) {
            throwWebException(message, args);
        }
    }

    public void notEmpty(float[] array) {
        notEmpty(array, ILLEGAL_PARAMETER);
    }

    public void notEmpty(float[] array, String message, Object... args) {
        if (array == null || array.length == 0) {
            throwWebException(message, args);
        }
    }

    public void notEmpty(double[] array) {
        notEmpty(array, ILLEGAL_PARAMETER);
    }

    public void notEmpty(double[] array, String message, Object... args) {
        if (array == null || array.length == 0) {
            throwWebException(message, args);
        }
    }

    public void notEmpty(Collection<?> collection) {
        notEmpty(collection, ILLEGAL_PARAMETER);
    }

    public void notEmpty(Collection<?> collection, String message, Object... args) {
        if (collection == null || collection.size() == 0) {
            throwWebException(message, args);
        }
    }

    public void notEmpty(Map<?, ?> map) {
        notEmpty(map, ILLEGAL_PARAMETER);
    }

    public void notEmpty(Map<?, ?> map, String message, Object... args) {
        if (map == null || map.size() == 0) {
            throwWebException(message, args);
        }
    }

    public void failed(){
        failed(ILLEGAL_PARAMETER);
    }

    public void failed(String message, Object... args){
        throwWebException(message, args);
    }

    private void throwWebException(String message, Object... args){
        WebException exception = new WebException(message, args);
        exception.setSupportI18n(supportI18n);
        exception.setError("");
        throw exception;
    }

}
